<?php

class Ext_Cropzoom_Adapter_Gd
{ 
    public $dstImg;
    protected $imageSize;
    protected $imgType;

    function __construct()
    {
    }
    
    function setSourceImage($srcImg)
    {
        if(!file_exists($srcImg))
        {
            die("Image not exists.");
        }
        
        $this->imgType = $this->getImgType($srcImg);
        $function = $this->returnCorrectFunction($this->imgType);
        $this->image = $function($srcImg);
    }
    
    function setDestinationImage($dstImg)
    {
        Ext_Utility_File::prepareDir(dirname($dstImg));
        $this->dstImg = $dstImg;
    }

    function getImgWidth($src)
    {
        return imagesx($src);
    }

    function getImgHeight($src)
    {
        return imagesy($src);
    }
    
    function getImgSize($src)
    {
        if($this->imageSize)
        {
            return $this->imageSize;
        }
        $this->imageSize = getimagesize($src);
        return $this->imageSize;
    }
        
    function getImgType($src)
    {
        if($this->imgType)
        {
            return $this->imgType;
        }
        
        $typelist = array("1"=>"gif","2"=>"jpg","3"=>"png","4"=>"swf","5" => "psd","6"=>"bmp","15"=>"wbmp");
        
        $this->imgType = "";
        $imageSize = $this->getImgSize($src);
        if(isset($typelist[$imageSize[2]]))
        {
            $this->imgType = $typelist[$imageSize[2]];
        }
        
        return $this->imgType;
    }
        
    function graphics($settings)
    {

        $viewPortW = $settings["viewPortW"];
        $viewPortH = $settings["viewPortH"];
        $pWidth = $settings["imageW"];
        $pHeight =  $settings["imageH"];
        
        $width = $this->getImgWidth($this->image);
        $height = $this->getImgHeight($this->image);
        
        $image = $this->image;
        
        $ext = $this->getImgType($image);
        
        // Resample
        $image_p = imagecreatetruecolor($pWidth, $pHeight);
        $this->setTransparency($image,$image_p, $ext);
        imagecopyresampled($image_p, $image, 0, 0, 0, 0, $pWidth, $pHeight, $width, $height);
        imagedestroy($image);
        $widthR = imagesx($image_p);
        $hegihtR = imagesy($image_p);
        
        $selectorX = $settings["selectorX"];
        $selectorY = $settings["selectorY"];
        
        if($settings["imageRotate"]){
            $angle = 360 - $settings["imageRotate"];
            $image_p = imagerotate($image_p,$angle,0);
            
            $pWidth = imagesx($image_p);
            $pHeight = imagesy($image_p);
            
            //print $pWidth."---".$pHeight;
        
            $diffW = abs($pWidth - $widthR) / 2;
            $diffH = abs($pHeight - $hegihtR) / 2;
                
            $settings["imageX"] = ($pWidth > $widthR ? $settings["imageX"] - $diffW : $settings["imageX"] + $diffW);
            $settings["imageY"] = ($pHeight > $hegihtR ? $settings["imageY"] - $diffH : $settings["imageY"] + $diffH);
        }
        
        $src_y = $src_x = $dst_y = $dst_x = 0;
        
        if($settings["imageX"] > 0){
            $dst_x = abs($settings["imageX"]);
        }else{
            $src_x = abs($settings["imageX"]);
        }
        if($settings["imageY"] > 0){
            $dst_y = abs($settings["imageY"]);
        }else{
            $src_y = abs($settings["imageY"]);
        }
        
        $viewport = imagecreatetruecolor($settings["viewPortW"],$settings["viewPortH"]);
        $this->setTransparency($image_p,$viewport,$ext);
        
        imagecopy($viewport, $image_p, $dst_x, $dst_y, $src_x, $src_y, $pWidth, $pHeight);
        imagedestroy($image_p);
        
        
        //$selector = imagecreatetruecolor(100, 100);
        $selector = imagecreatetruecolor($settings["selectorW"],$settings["selectorH"]);
        
        $this->setTransparency($viewport,$selector,$ext);
        imagecopy($selector, $viewport, 0, 0, $selectorX, $selectorY,$settings["viewPortW"],$settings["viewPortH"]);
        
        $this->parseImage($selector);
        imagedestroy($viewport);
        //Return value
        return "";
        /* Functions */        
    }
    
    function determineImageScale($sourceWidth, $sourceHeight, $targetWidth, $targetHeight) {
        $scalex =  $targetWidth / $sourceWidth;
        $scaley =  $targetHeight / $sourceHeight;
        return min($scalex, $scaley);
    }
    
    function returnCorrectFunction($ext){
        $function = "";
        switch($ext){
            case "png":
                $function = "imagecreatefrompng";
                break;
            case "jpeg":
                $function = "imagecreatefromjpeg";
                break;
            case "jpg":
                $function = "imagecreatefromjpeg";
                break;
            case "gif":
                $function = "imagecreatefromgif";
                break;
        }
        return $function;
    }
    
    function parseImage($img){
        $file = $this->dstImg;

        switch($this->imgType){
            case "png":
                imagepng($img,($file != null ? $file : ''));
                break;
            case "jpeg":
                imagejpeg($img,($file ? $file : ''),90);
                break;
            case "jpg":
                imagejpeg($img,($file ? $file : ''),90);
                break;
            case "gif":
                imagegif($img,($file ? $file : ''));
                break;
        }
    }
    
    function setTransparency($imgSrc,$imgDest,$ext){
    
        if($ext == "png" || $ext == "gif"){
            $trnprt_indx = imagecolortransparent($imgSrc);
            // If we have a specific transparent color
            if ($trnprt_indx >= 0) {
                // Get the original image's transparent color's RGB values
                $trnprt_color    = imagecolorsforindex($imgSrc, $trnprt_indx);
                // Allocate the same color in the new image resource
                $trnprt_indx    = imagecolorallocate($imgDest, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
                // Completely fill the background of the new image with allocated color.
                imagefill($imgDest, 0, 0, $trnprt_indx);
                // Set the background color for new image to transparent
                imagecolortransparent($imgDest, $trnprt_indx);
            }
            // Always make a transparent background color for PNGs that don't have one allocated already
            elseif ($ext == "png") {
                // Turn off transparency blending (temporarily)
                imagealphablending($imgDest, true);
                // Create a new transparent color for image
                $color = imagecolorallocatealpha($imgDest, 0, 0, 0, 127);
                // Completely fill the background of the new image with allocated color.
                imagefill($imgDest, 0, 0, $color);
                // Restore transparency blending
                imagesavealpha($imgDest, true);
            }
    
        }
    }
}
